package com.zhangdi.zddemo.thread;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2023.8.16
 */
public class ThreadPoolTest {
    public static void main(String[] args) {
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2, 3, 60,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(1),
                new ThreadPoolExecutor.AbortPolicy());

        poolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                boolean isInterrupt = false;
                int i = 0;
                while (true) {
                    try {
                        if(isInterrupt){
                           return;
                        }

                        System.out.println("running...");
                        Thread.sleep(1000);
                        ++i;
                        if (i == 5) {
                            int f = 1 / 0;
                        }
                    }catch (Exception e) {
                        e.printStackTrace();
                        isInterrupt = true;
                    }
                }
            }
        });

        poolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(poolExecutor.getPoolSize() + " " + poolExecutor.getActiveCount() + " " + poolExecutor.getTaskCount() + " " + poolExecutor.getCompletedTaskCount());
                }
            }
        });

        poolExecutor.execute(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    System.out.println(3333333);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });
//        thread.start();

    }
}
